package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryDocumentReq;
import com.sg.entity.OmsUnstructuredData;
import com.sg.service.base.app.MOmsUnstructuredDataService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsUnstructuredDataMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsUnstructuredDataServiceImpl
        extends ServiceImpl<OmsUnstructuredDataMapper, OmsUnstructuredData>
        implements MOmsUnstructuredDataService {

    @Autowired
    OmsUnstructuredDataMapper omsUnstructuredDataMapper;

    /**
     * 2-7-01批量删文档
     */
    @Trace(operationName = "2-7-01批量删文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteDocument(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-7-01修改文档
     */
    @Trace(operationName = "2-7-01修改文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDocument(OmsUnstructuredData omsUnstructuredData) {
        omsUnstructuredData.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredData.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsUnstructuredData);
    }

    /**
     * 2-7-01新增文档
     */
    @Trace(operationName = "2-7-01新增文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addDocument(OmsUnstructuredData omsUnstructuredData) {
        omsUnstructuredData.setUnstructuredDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsUnstructuredData.setIsArchive("FALSE");
        omsUnstructuredData.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredData.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsUnstructuredData != null && omsUnstructuredData.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsUnstructuredData.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsUnstructuredData.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsUnstructuredData.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsUnstructuredData != null && omsUnstructuredData.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsUnstructuredData.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsUnstructuredData.setIsValid("FALSE");
        omsUnstructuredData.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsUnstructuredData.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsUnstructuredData)) {
            return omsUnstructuredData.getUnstructuredDataId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-7-01批量查询文档列表
     */
    @Trace(operationName = "2-7-01批量查询文档列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredData> batchQueryDocument(BatchQueryDocumentReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredData> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getDocumentList() == null || reqDto.getDocumentList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("unstructured_data_id", reqDto.getDocumentList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsUnstructuredData>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
